package cn.yunhe.controller;

import cn.yunhe.entity.Department;
import cn.yunhe.service.impl.DepartmentServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DepartmentController {

    @Resource
    private DepartmentServiceImpl departmentService;

    @RequestMapping("getAllDep")
    public List<Department> getAllDep(){
        return departmentService.getAllDep();
    }



    @RequestMapping("departmentPage")
    public Map queryNewsByPage(int pageSize, int pageNum, Department department){
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("department", department);
        Page page = departmentService.getDepartmentListByPage(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }

    @RequestMapping("/addDepartment")
    public int addNews(Department department) {
        return departmentService.addDepartment(department);
    }

    @RequestMapping("/delDepartment")
    public int delNews(Department department) {
        System.out.println("id " +department.getDepId());
        return departmentService.delDepartment(department);
    }

    @RequestMapping("/getDepartmentById")
    public ModelAndView getNewsById(Department department, HttpServletRequest request) {
        Department d = departmentService.getDepartmentById(department);
        request.setAttribute("data", d);
        return new ModelAndView("updDepartment");
    }

    @RequestMapping("/updDepartment")
    public int updDepartment(Department department) {
        return departmentService.updDepartment(department);
    }




    public DepartmentServiceImpl getDepartmentService() {
        return departmentService;
    }

    public void setDepartmentService(DepartmentServiceImpl departmentService) {
        this.departmentService = departmentService;
    }
}
